package com.info.zhiduoduo.pay.sdk.dayspaysdk.core.util;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * ���� ʱ�������
 * @author xie
 *
 */
public class DateUtil {

	private static Logger logger = Logger.getLogger(DateUtil.class.getName());

	/**
	 * ��ȡ����ʱ��ĺ����
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getMilliseconds(Date a, Date b){
		return Math.abs(a.getTime() - b.getTime());
	}

	/**
	 * ������now��������days
	 * @param now
	 * @param days
	 * @return �ַ���
	 */
	public static Date addDays(Date now, int days){
		Date rs = new Date();
		rs.setTime(now.getTime() + days*1000L * 60 * 60 * 24);
		return rs;
	}

	/**
	 * ��ȡ����ʱ����ղ�
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInDay(Date a, Date b){

		long quot = 0;
		try {
			quot = a.getTime() - b.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return Math.abs(quot);
	}

	/**
	 * ��ȡ����ʱ���ַ������ղ�
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInDay_yyyyMMdd(String a, String b){

		long quot = 0;
		try {
			Date date1 = yyyyMMdd(a);
			Date date2 = yyyyMMdd(b);
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return Math.abs(quot);
	}
	/**
	 * ��ȡ����ʱ���ַ������ղ�
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInDay_yyyy_MM_dd(String a, String b){

		long quot = 0;
		try {
			Date date1 = yyyy_MM_dd(a);
			Date date2 = yyyy_MM_dd(b);
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return Math.abs(quot);
	}

	/**
	 * ��ȡ����ʱ����ղ�
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInDayNoAbs(Date big, Date small){

		long quot = 0;
		try {
			quot = big.getTime() - small.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return quot;
	}

	/**
	 * ��ȡ����ʱ��ķ��Ӳ�
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInMin(Date a, Date b){

		long quot = 0;
		try {
			quot = Math.abs(a.getTime() - b.getTime());
			quot = quot / 1000 / 60;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return Math.abs(quot);
	}
	/**
	 * ��ȡ����ʱ���ַ����ķ��Ӳ�
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInMin(String a, String b){

		long quot = 0;
		try {
			Date date1 = yyyy_MM_dd_HH_mm_ss(a);
			Date date2 = yyyy_MM_dd_HH_mm_ss(b);
			quot = Math.abs(date1.getTime() - date2.getTime());
			quot = quot / 1000 / 60;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return Math.abs(quot);
	}

	/**
	 * ��ȡ����ʱ���ַ��������
	 * @param a
	 * @param b
	 * @return �ַ���
	 */
	public static long getDisInSec(String a, String b){

		long quot = 0;
		try {
			Date date1 = yyyy_MM_dd_HH_mm_ss(a);
			Date date2 = yyyy_MM_dd_HH_mm_ss(b);
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000;
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}
		return Math.abs(quot);
	}

	//beg&end format excemple:00:10,23:10
	/**
	 * �жϵ�ǰʱ���Ƿ���[beg,end]ʱ��������
	 * @param beg ��ʼʱ�֣����磺00:10��0��10�ֿ�ʼ
	 * @param end ����ʱ�֣�����23:10��23��10�ֽ���
	 * @return �ַ���
	 */
	public static boolean isNowInTimeZone(String beg,String end) {


		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int min = Calendar.getInstance().get(Calendar.MINUTE);

		if(hour < Integer.parseInt(beg.split(":")[0]) || (hour == Integer.parseInt(beg.split(":")[0]) && min <= Integer.parseInt(beg.split(":")[1])))
			return false;
		if(hour > Integer.parseInt(end.split(":")[0]) || (hour == Integer.parseInt(end.split(":")[0]) && min >= Integer.parseInt(end.split(":")[1])))
			return false;

		return true;
	}

	/**
	 * ��ʱ��time��ʽ��ΪHHmmss��ʽ
	 * @param time
	 * @return �ַ���
	 */
	public static String HHmmss(Date time) {
		if (time == null) {
			return "";
		}
		SimpleDateFormat hhmmss = new SimpleDateFormat("HHmmss");
		return hhmmss.format(time);
	}

	/**
	 * ��ʱ��time��ʽ��ΪHH:mm��ʽ
	 * @param time
	 * @return �ַ���
	 */
	public static String HH_mm(Date time) {
		if (time == null) {
			return "";
		}
		SimpleDateFormat hh1mm = new SimpleDateFormat("HH:mm");
		return hh1mm.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪMMddHHmmss��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String MMddHHmmss(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat mmddHHmmss = new SimpleDateFormat("MMddHHmmss");
		return mmddHHmmss.format(date);
	}

	/**
	 * ��ʱ��date��ʽ��ΪMMdd��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String MMdd(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat mmdd = new SimpleDateFormat("MMdd");
		return mmdd.format(date);
	}

	/**
	 * ��ʱ��date��ʽ��Ϊyyyy��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyyy(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
		return yyyy.format(date);
	}

	/**
	 * ��ʱ��date��ʽ��ΪyyyyMM��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyyyMM(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
		return yyyyMM.format(date);
	}

	/**
	 * ��ʱ��date��ʽ��Ϊyyyy-MM-dd��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyyy_MM_dd(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat yyyy1MM1dd = new SimpleDateFormat("yyyy-MM-dd");
		return yyyy1MM1dd.format(date);
	}

	/**
	 * ��ʱ��date��ʽ��ΪyyMMdd��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyMMdd(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat yyMMdd = new SimpleDateFormat("yyMMdd");
		return yyMMdd.format(date);
	}

	/**
	 * ��ʱ��date��ʽ��Ϊyy��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yy(Date year) {
		if (year == null) {
			return "";
		}
		SimpleDateFormat yy = new SimpleDateFormat("yy");
		return yy.format(year);
	}

	/**
	 * ��ʱ��date��ʽ��Ϊyyyy-MM-dd HH:mm:ss��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyyy_MM_dd_HH_mm_ss(Date time) {
		if (time == null) {
			return "";
		}
		SimpleDateFormat yyyy1MM1dd1HH1mm1ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return yyyy1MM1dd1HH1mm1ss.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪyyMMddHHmmss��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyMMddHHmmss(Date time) {
		if (time == null) {
			return "";
		}
		SimpleDateFormat yyMMddHHmmss = new SimpleDateFormat("yyMMddHHmmss");
		return yyMMddHHmmss.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪyyyyMMdd��ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyyyMMdd(Date time) {
		if (time == null) {
			return "";
		}
		SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
		return yyyyMMdd.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��Ϊyyyy��MM��dd�� HHʱmm��ss���ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String china_yyyy_MM_dd_HH_mm_ss(Date time) {
		SimpleDateFormat china1yyyy1MM1dd1HH1mm1ss = new SimpleDateFormat("yyyy��MM��dd�� HHʱmm��ss��");
		return china1yyyy1MM1dd1HH1mm1ss.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪMM��dd�����ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String china_MM_dd(Date time) {
		SimpleDateFormat china1MM1dd = new SimpleDateFormat("MM��dd��");
		return china1MM1dd.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪHH:mm:ss.SSS�����ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String HH_mm_ss_SSS(Date time) {
		SimpleDateFormat hh1mm1ss1SSS = new SimpleDateFormat("HH:mm:ss.SSS");
		return hh1mm1ss1SSS.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪHH:mm:ss�����ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String HH_mm_ss(Date time) {
		SimpleDateFormat hh1mm1ss = new SimpleDateFormat("HH:mm:ss");
		return hh1mm1ss.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪHHmmssSSS�����ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String HHmmssSSS(Date time) {
		SimpleDateFormat hhmmssSSS = new SimpleDateFormat("HHmmssSSS");
		return hhmmssSSS.format(time);
	}

	/**
	 * ��ʱ��date��ʽ��ΪyyyyMMddHHmmss�����ʽ
	 * @param date
	 * @return �ַ���
	 */
	public static String yyyyMMddHHmmss(Date time) {
		SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
		if(null == time)
			return "";
		return yyyyMMddHHmmss.format(time);
	}

	/**
	 * ��yyyyMMdd��ʽ���ַ���תΪ������
	 * @param date
	 * @return ������
	 */
	public static Date yyyyMMdd(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
		return yyyyMMdd.parse(date);
	}

	/**
	 * ��yyyy-MM-dd HH:mm:ss��ʽ���ַ���תΪ������
	 * @param date
	 * @return ������
	 */
	public static Date yyyy_MM_dd_HH_mm_ss(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat yyyy1MM1dd1HH1mm1ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return yyyy1MM1dd1HH1mm1ss.parse(date);
	}

	/**
	 * ��yyyyMMddHHmmss��ʽ���ַ���תΪ������
	 * @param date
	 * @return ������
	 */
	public static Date yyyyMMddHHmmss(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
		return yyyyMMddHHmmss.parse(date);
	}

	/**
	 * ��MMddHHmmss��ʽ���ַ���תΪ�����࣬���ȡ��ǰ����ʱ������
	 * @param date
	 * @return ������
	 */
	public static Date MMddHHmmss(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		return yyyyMMddHHmmss(Calendar.getInstance().get(Calendar.YEAR) + date);
	}

	/**
	 * ��yyyy-MM-dd��ʽ���ַ���תΪ������
	 * @param date
	 * @return ������
	 */
	public static Date yyyy_MM_dd(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat yyyy1MM1dd = new SimpleDateFormat("yyyy-MM-dd");
		return yyyy1MM1dd.parse(date);
	}

	/**
	 * ��HH:mm:ss��ʽ���ַ���תΪ������
	 * @param date
	 * @return ������
	 */
	public static Date HH_mm_ss(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat hh1mm1ss = new SimpleDateFormat("HH:mm:ss");
		return hh1mm1ss.parse(date);
	}

	/**
	 * ��EEE MMM dd HH:mm:ss zzz yyyy��ʽ���ַ���תΪ������
	 * @param date
	 * @return ������
	 */
	public static Date EEEMMM(String date) throws ParseException {
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat eeemmm = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
		return eeemmm.parse(date);
	}

	/**
	 * ���ڲ��ֽ��ڻ����������������Ľ�����������ֻ��MMdd��Ϊ���������꣬������Ҫ������������㽻������������������
	 * @param transTime ����ʱ��
	 * @param mmdd �����ɽ��ڻ������ص���������
	 * @return �������ڣ�yyyyMMdd��ʽ
	 */
	public static Date toSettDate(Date transTime, String mmdd) throws ParseException{
		Date date = null;
		String settleDate = "";
		Calendar orderTime = Calendar.getInstance();
		orderTime.setTime(transTime);
		Calendar nextdate = Calendar.getInstance();
		nextdate.setTime(transTime);
		nextdate.add(Calendar.DAY_OF_MONTH, 1);
		if((nextdate.get(Calendar.YEAR) > orderTime.get(Calendar.YEAR)) && (!MMdd(orderTime.getTime()).equals(mmdd))){
			settleDate = nextdate.get(Calendar.YEAR) + "" + mmdd;
		} else {
			settleDate = orderTime.get(Calendar.YEAR) + "" + mmdd;
		}
		date = yyyyMMdd(settleDate);
		return date;
	}
	
	public static Date pattenToDate(String patten, String date) throws ParseException{
		if (Strings.isNullOrEmpty(date)) {
			return null;
		}
		SimpleDateFormat fmt = new SimpleDateFormat(patten);
		return fmt.parse(date);
	}
	
	public static Date fmt_yyyyMMddHHmmss(Date datetime) throws ParseException {
		return yyyyMMddHHmmss(yyyyMMddHHmmss(datetime));
	}
	
	public static String padSettleDate(String settDate) throws ParseException {
		
		if(settDate.length() == 4) {
			Calendar now = Calendar.getInstance();
			String year = DateUtil.yyyy(now.getTime());
			String date = DateUtil.MMdd(now.getTime());
			long dis = DateUtil.getDisInDay(now.getTime(),DateUtil.yyyyMMdd(DateUtil.yyyy(now.getTime())+settDate));
			System.out.println(dis);
			if(dis > 1 && "0101".equals(date)) {//����
				now.add(Calendar.YEAR,-1);
				year = DateUtil.yyyy(now.getTime());
			}
			settDate = year+settDate;
		}
		
		return settDate;
	}
	
	public static void main(String[] args) throws ParseException {
		
		System.out.println(padSettleDate("0102"));
	}
}
